package com.kd.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kd.entity.Product;
import lombok.Data;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface ProductMapper extends BaseMapper<Product> {

    @Select("<script>" +
            "SELECT pid, name, sidefork, panel_leg, customize, psq " +  // 已去掉末尾逗号
            "FROM product " +
            "<where>" +
            "  <if test='cursor != null'>" +
            "    AND psq <![CDATA[${isNext ? '<' : '>'}]]> #{cursor} " +
            "  </if>" +
            "</where>" +
            "ORDER BY psq DESC " +
            "LIMIT #{limit}" +
            "</script>")
    List<ProductStats> selectProductStats(
            @Param("cursor") Integer cursor,  // 分页游标（取上一页最后一条的psq值）
            @Param("limit") int limit,        // 每页条数
            @Param("isNext") boolean isNext   // true=下一页，false=上一页
    );


    @Update("UPDATE product SET " +
            "psq = psq + #{quantityDelta} " +
            "WHERE pid = #{pid}")
    int updateProductStats(
            @Param("pid") Integer pid,
            @Param("quantityDelta") int quantityDelta
    );


    @Data
    public class ProductStats {
        private Integer pid;          // 保持与数据库一致
        private String name;
        private String sidefork;
        private String panel_leg;
        private String customize;
        private Integer psq;
        // 游标计算方法（返回Integer类型）
        public Integer getCursor() {
            return psq;  // 直接返回psq值作为游标
        }
    }
}